// Copyright (c) MatrixFramework. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.

using System.Collections.Generic;

namespace MatrixFramework.Keycloak.Abstractions.Models;

/// <summary>
/// Realm创建信息
/// </summary>
public class RealmCreationInfo
{
    /// <summary>
    /// Realm名称
    /// </summary>
    public string RealmName { get; set; } = string.Empty;

    /// <summary>
    /// 显示名称
    /// </summary>
    public string? DisplayName { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    public string? Description { get; set; }

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool Enabled { get; set; } = true;

    /// <summary>
    /// 管理员用户信息
    /// </summary>
    public UserCreationInfo? AdminUser { get; set; }

    /// <summary>
    /// 默认角色列表
    /// </summary>
    public List<string> DefaultRoles { get; set; } = new();

    /// <summary>
    /// 主题配置
    /// </summary>
    public ThemeConfiguration? Theme { get; set; }

    /// <summary>
    /// 附加属性
    /// </summary>
    public Dictionary<string, object> Attributes { get; set; } = new();
}

/// <summary>
/// 用户创建信息
/// </summary>
public class UserCreationInfo
{
    /// <summary>
    /// 用户名
    /// </summary>
    public string Username { get; set; } = string.Empty;

    /// <summary>
    /// 邮箱
    /// </summary>
    public string Email { get; set; } = string.Empty;

    /// <summary>
    /// 名
    /// </summary>
    public string FirstName { get; set; } = string.Empty;

    /// <summary>
    /// 姓
    /// </summary>
    public string LastName { get; set; } = string.Empty;

    /// <summary>
    /// 密码
    /// </summary>
    public string Password { get; set; } = string.Empty;

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool Enabled { get; set; } = true;

    /// <summary>
    /// 邮箱是否已验证
    /// </summary>
    public bool EmailVerified { get; set; } = false;

    /// <summary>
    /// 角色列表
    /// </summary>
    public List<string> Roles { get; set; } = new();

    /// <summary>
    /// 组列表
    /// </summary>
    public List<string> Groups { get; set; } = new();

    /// <summary>
    /// 附加属性
    /// </summary>
    public Dictionary<string, object> Attributes { get; set; } = new();

    /// <summary>
    /// 租户ID（多租户场景）
    /// </summary>
    public string? TenantId { get; set; }

    /// <summary>
    /// 创建时间
    /// </summary>
    public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
}

/// <summary>
/// 用户更新信息
/// </summary>
public class UserUpdateInfo
{
    /// <summary>
    /// 邮箱
    /// </summary>
    public string? Email { get; set; }

    /// <summary>
    /// 名
    /// </summary>
    public string? FirstName { get; set; }

    /// <summary>
    /// 姓
    /// </summary>
    public string? LastName { get; set; }

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool? Enabled { get; set; }

    /// <summary>
    /// 邮箱是否已验证
    /// </summary>
    public bool? EmailVerified { get; set; }

    /// <summary>
    /// 附加属性
    /// </summary>
    public Dictionary<string, object>? Attributes { get; set; }
}

/// <summary>
/// 角色创建信息
/// </summary>
public class RoleCreationInfo
{
    /// <summary>
    /// 角色名称
    /// </summary>
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 角色描述
    /// </summary>
    public string? Description { get; set; }

    /// <summary>
    /// 是否为复合角色
    /// </summary>
    public bool Composite { get; set; } = false;

    /// <summary>
    /// 客户端角色
    /// </summary>
    public bool ClientRole { get; set; } = false;

    /// <summary>
    /// 容器ID
    /// </summary>
    public string? ContainerId { get; set; }

    /// <summary>
    /// 关联角色
    /// </summary>
    public List<string> Composites { get; set; } = new();

    /// <summary>
    /// 附加属性
    /// </summary>
    public Dictionary<string, object> Attributes { get; set; } = new();
}

/// <summary>
/// 客户端创建信息
/// </summary>
public class ClientCreationInfo
{
    /// <summary>
    /// 客户端ID
    /// </summary>
    public string ClientId { get; set; } = string.Empty;

    /// <summary>
    /// 客户端名称
    /// </summary>
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 客户端协议
    /// </summary>
    public string Protocol { get; set; } = "openid-connect";

    /// <summary>
    /// 访问类型
    /// </summary>
    public ClientAccessType AccessType { get; set; } = ClientAccessType.Confidential;

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool Enabled { get; set; } = true;

    /// <summary>
    /// 重定向URI列表
    /// </summary>
    public List<string> RedirectUris { get; set; } = new();

    /// <summary>
    /// 允许的来源
    /// </summary>
    public List<string> WebOrigins { get; set; } = new();

    /// <summary>
    /// 默认角色
    /// </summary>
    public List<string> DefaultRoles { get; set; } = new();

    /// <summary>
    /// 授权设置
    /// </summary>
    public AuthorizationSettings? Authorization { get; set; }

    /// <summary>
    /// 附加属性
    /// </summary>
    public Dictionary<string, object> Attributes { get; set; } = new();
}

/// <summary>
/// 权限作用域创建信息
/// </summary>
public class ScopeCreationInfo
{
    /// <summary>
    /// 作用域名称
    /// </summary>
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 显示名称
    /// </summary>
    public string? DisplayName { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    public string? Description { get; set; }

    /// <summary>
    /// 图标URI
    /// </summary>
    public string? IconUri { get; set; }

    /// <summary>
    /// 包含的协议映射器
    /// </summary>
    public List<string> ProtocolMappers { get; set; } = new();

    /// <summary>
    /// 包含的角色
    /// </summary>
    public List<string> Roles { get; set; } = new();

    /// <summary>
    /// 附加属性
    /// </summary>
    public Dictionary<string, object> Attributes { get; set; } = new();
}

/// <summary>
/// 用户搜索请求
/// </summary>
public class UserSearchRequest
{
    /// <summary>
    /// 搜索关键词
    /// </summary>
    public string? Search { get; set; }

    /// <summary>
    /// 邮箱
    /// </summary>
    public string? Email { get; set; }

    /// <summary>
    /// 用户名
    /// </summary>
    public string? Username { get; set; }

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool? Enabled { get; set; }

    /// <summary>
    /// 邮箱是否已验证
    /// </summary>
    public bool? EmailVerified { get; set; }

    /// <summary>
    /// 页码（从1开始）
    /// </summary>
    public int Page { get; set; } = 1;

    /// <summary>
    /// 页大小
    /// </summary>
    public int PageSize { get; set; } = 20;

    /// <summary>
    /// 排序字段
    /// </summary>
    public string? SortBy { get; set; }

    /// <summary>
    /// 排序方向
    /// </summary>
    public SortDirection SortDirection { get; set; } = SortDirection.Ascending;
}

/// <summary>
/// 租户信息
/// </summary>
public class TenantInfo
{
    /// <summary>
    /// 租户ID
    /// </summary>
    public string Id { get; set; } = string.Empty;

    /// <summary>
    /// 租户名称
    /// </summary>
    public string Name { get; set; } = string.Empty;

    /// <summary>
    /// 租户代码
    /// </summary>
    public string Code { get; set; } = string.Empty;

    /// <summary>
    /// 描述
    /// </summary>
    public string? Description { get; set; }

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool Enabled { get; set; } = true;

    /// <summary>
    /// 管理员用户信息
    /// </summary>
    public UserCreationInfo? AdminUser { get; set; }

    /// <summary>
    /// 附加配置
    /// </summary>
    public Dictionary<string, object> Configuration { get; set; } = new();
}

/// <summary>
/// Realm配置
/// </summary>
public class RealmConfiguration
{
    /// <summary>
    /// 显示名称
    /// </summary>
    public string? DisplayName { get; set; }

    /// <summary>
    /// 描述
    /// </summary>
    public string? Description { get; set; }

    /// <summary>
    /// 是否启用
    /// </summary>
    public bool? Enabled { get; set; }

    /// <summary>
    /// 主题配置
    /// </summary>
    public ThemeConfiguration? Theme { get; set; }

    /// <summary>
    /// 安全配置
    /// </summary>
    public SecurityConfiguration? Security { get; set; }

    /// <summary>
    /// 令牌配置
    /// </summary>
    public TokenConfiguration? Tokens { get; set; }

    /// <summary>
    /// 附加属性
    /// </summary>
    public Dictionary<string, object>? Attributes { get; set; }
}

/// <summary>
/// 主题配置
/// </summary>
public class ThemeConfiguration
{
    /// <summary>
    /// 登录主题
    /// </summary>
    public string? LoginTheme { get; set; }

    /// <summary>
    /// 账户主题
    /// </summary>
    public string? AccountTheme { get; set; }

    /// <summary>
    /// 管理主题
    /// </summary>
    public string? AdminTheme { get; set; }

    /// <summary>
    /// 邮件主题
    /// </summary>
    public string? EmailTheme { get; set; }
}

/// <summary>
/// 安全配置
/// </summary>
public class SecurityConfiguration
{
    /// <summary>
    /// 密码策略
    /// </summary>
    public string? PasswordPolicy { get; set; }

    /// <summary>
    /// 是否启用多因子认证
    /// </summary>
    public bool? OtpEnabled { get; set; }

    /// <summary>
    /// 密码最小长度
    /// </summary>
    public int? PasswordMinLength { get; set; }

    /// <summary>
    /// 是否记住用户
    /// </summary>
    public bool? RememberMe { get; set; }
}

/// <summary>
/// 令牌配置
/// </summary>
public class TokenConfiguration
{
    /// <summary>
    /// 访问令牌生存时间（秒）
    /// </summary>
    public int? AccessTokenLifespan { get; set; }

    /// <summary>
    /// 刷新令牌最大空闲时间（秒）
    /// </summary>
    public int? RefreshTokenMaxReuse { get; set; }

    /// <summary>
    /// 刷新令牌生存时间（秒）
    /// </summary>
    public int? RefreshTokenLifespan { get; set; }

    /// <summary>
    /// SSO会话最大生存时间（秒）
    /// </summary>
    public int? SsoSessionMaxLifespan { get; set; }

    /// <summary>
    /// SSO会话空闲时间（秒）
    /// </summary>
    public int? SsoSessionIdleRememberMe { get; set; }
}

/// <summary>
/// 授权设置
/// </summary>
public class AuthorizationSettings
{
    /// <summary>
    /// 是否启用授权
    /// </summary>
    public bool Enabled { get; set; } = false;

    /// <summary>
    /// 决策策略
    /// </summary>
    public string DecisionStrategy { get; set; } = "UNANIMOUS";

    /// <summary>
    /// 权限资源设置
    /// </summary>
    public List<string> Resources { get; set; } = new();

    /// <summary>
    /// 策略设置
    /// </summary>
    public List<string> Policies { get; set; } = new();

    /// <summary>
    /// 作用域设置
    /// </summary>
    public List<string> Scopes { get; set; } = new();
}

/// <summary>
/// 客户端访问类型
/// </summary>
public enum ClientAccessType
{
    /// <summary>
    /// 公开客户端
    /// </summary>
    Public = 0,

    /// <summary>
    /// 保密客户端
    /// </summary>
    Confidential = 1,

    /// <summary>
    /// 仅承载客户端
    /// </summary>
    BearerOnly = 2
}

/// <summary>
/// 排序方向
/// </summary>
public enum SortDirection
{
    /// <summary>
    /// 升序
    /// </summary>
    Ascending = 0,

    /// <summary>
    /// 降序
    /// </summary>
    Descending = 1
}